package com.hddz.acquisition.view;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hddz.acquisition.R;
import com.nineoldandroids.view.ViewHelper;

/**
 * 用于GridView装载数据的适配器
 * 
 * @author xxs
 * 
 */
public class MenuAdapter extends BaseAdapter {
	private List<Menu> menus;// 定义一个list对象
	private Context mContext;// 上下文
	public static final float APP_PAGE_SIZE = 4.0f;// 每一页装载数据的大小
	private PackageManager pm;// 定义一个PackageManager对象

	/**
	 * 构造方法
	 * 
	 * @param context
	 *            上下文
	 * @param titles
	 *            所有APP的集合
	 * @param page
	 *            当前页
	 */
	public MenuAdapter(Context context, int[] titles, int[] icos, int page) {
		mContext = context;
		pm = context.getPackageManager();
		menus = new ArrayList<MenuAdapter.Menu>();
		// 根据当前页计算装载的应用，每页只装载16个
		int start = (int) (page * APP_PAGE_SIZE);// 当前页的其实位置
		for (int i = 0; i < APP_PAGE_SIZE; i++) {
			if (start + i < titles.length) {
				Menu menu = new Menu();
				menu.setIndex(i + (i > 1 ? 2 : 1));
				menu.setTitle(titles[start + i]);
				menu.setIco(icos[start + i]);
				menus.add(menu);
			} else {
				menus.add(null);
			}
		}
	}

	public MenuAdapter(Context context, int[] titles, int[] icos, Boolean[] enables, int page) {
		mContext = context;
		pm = context.getPackageManager();
		menus = new ArrayList<MenuAdapter.Menu>();
		// 根据当前页计算装载的应用，每页只装载16个
		int start = (int) (page * APP_PAGE_SIZE);// 当前页的其实位置
		for (int i = 0; i < APP_PAGE_SIZE; i++) {
			if (start + i < titles.length) {
				Menu menu = new Menu();
				menu.setIndex(i + (i > 1 ? 2 : 1));
				menu.setTitle(titles[start + i]);
				menu.setIco(icos[start + i]);
				menu.setEnable(enables[start + i]);
				menus.add(menu);
			} else {
				menus.add(null);
			}
		}
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		if (menus.size() > position && menus.get(position) != null)
			return menus.get(position).getEnable();
		else
			return false;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return menus.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return menus.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("NewApi")
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.view_menu_item, parent, false);
		}
		Menu menu = menus.get(position);
		if (menu != null) {
			TextView appindex = (TextView) convertView.findViewById(R.id.tvAppIndex);
			ImageView appicon = (ImageView) convertView.findViewById(R.id.ivAppIcon);
			TextView appname = (TextView) convertView.findViewById(R.id.tvAppName);
			appindex.setText(menu.getIndex() + "");
			appicon.setImageDrawable(mContext.getResources().getDrawable(menu.getIco()));
			appname.setText(mContext.getResources().getString(menu.getTitle()));
		}
		if (isEnabled(position)) {
			ViewHelper.setAlpha(convertView, 1f);
		} else {
			ViewHelper.setAlpha(convertView, 0.5f);
		}
		return convertView;
	}

	class Menu {
		private int ico;
		private int title;
		private int index;
		private Boolean enable = true;

		public int getIco() {
			return ico;
		}

		public void setIco(int ico) {
			this.ico = ico;
		}

		public int getTitle() {
			return title;
		}

		public void setTitle(int title) {
			this.title = title;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

		public Boolean getEnable() {
			return enable;
		}

		public void setEnable(Boolean enable) {
			this.enable = enable;
		}

	}
}
